﻿Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO
Imports DevExpress.XtraLayout
Imports DevExpress.XtraEditors

Public Class FormRiwayatKesehatan
    Inherits PageTemplate
    Dim isTambah As Boolean = False
    Dim vID As String = ""
    Dim vSAKIT As String = ""
    Dim vTGLMULAI As String = ""
    Dim vTGLSELESAI As String = ""
    Dim vKETERANGAN As String = ""

    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCARI.Click
        Dim dlgcari As New DialogCariSiswa
        dlgcari.Text = "Pencarian Data Siswa"
        ClearLayout("semua")
        'setControlAktif(False)
        If dlgcari.ShowDialog() = DialogResult.OK Then
            If Not dlgcari.VarNIS = String.Empty Then
                txtNIS.Text = dlgcari.VarNIS
                txtNAMA.Text = dlgcari.VarNAMA
                btnCARI_Click(sender, e)
                'txtUMUR_INPUT.Focus()
            End If
        End If

    End Sub

    Private Sub ClearLayout(ByVal kolom As String)
        If kolom = "semua" Then
            txtNIS.Text = ""
            txtNAMA.Text = ""
            btnSIMPAN.Enabled = False
            btnHAPUS.Enabled = False
        End If

        If kolom = "semua" Or kolom = "1" Then
            For i As Integer = 0 To LayoutControlGroup4.Items.Count - 1
                Dim item As New LayoutControlItem
                If Not LayoutControlGroup4.Items(i).IsGroup Then
                    item = LayoutControlGroup4.Items(i)
                    If TypeOf item.Control Is TextEdit Then
                        item.Control.Text = ""
                    ElseIf TypeOf item.Control Is DateEdit Then
                        item.Control.Text = ""
                    ElseIf TypeOf item.Control Is MemoEdit Then
                        item.Control.Text = ""
                    ElseIf TypeOf item.Control Is CheckEdit Then
                        Dim chkedit As New CheckEdit
                        chkedit = CType(item.Control, CheckEdit)
                        chkedit.Checked = 0
                    End If
                End If
            Next
        End If

        If kolom = "semua" Or kolom = "2" Then
            For i As Integer = 0 To LayoutControlGroup5.Items.Count - 1
                Dim item As New LayoutControlItem
                If Not LayoutControlGroup5.Items(i).IsGroup Then
                    item = LayoutControlGroup5.Items(i)
                    If TypeOf item.Control Is DateEdit Then
                        Dim deedit As New DateEdit
                        deedit = CType(item.Control, DateEdit)
                        deedit.Text = ""
                    ElseIf TypeOf item.Control Is TextEdit Then
                        item.Control.Text = ""
                    ElseIf TypeOf item.Control Is MemoEdit Then
                        item.Control.Text = ""
                    ElseIf TypeOf item.Control Is CheckEdit Then
                        Dim chkedit As New CheckEdit
                        chkedit = CType(item.Control, CheckEdit)
                        chkedit.Checked = 0
                    End If
                End If
            Next
        End If

    End Sub

    Private Sub btnCARI_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim dtsiswa As New DataTable

        GetLoadDataSiswa(dtsiswa)
        If Utils.RowExist(dtsiswa) Then            
            btnTAMBAH.Enabled = True
            setControlAktif(2)

            Dim dtriwayatkesehatansiswa As New DataTable

            GetLoadDataRiwayatSiswa(dtriwayatkesehatansiswa)
            refreshGrid(dtriwayatkesehatansiswa)

        Else
            MsgBox("Data Siswa Tidak Ditemukan.", MsgBoxStyle.OkOnly, "Informasi")
            Return
        End If
    End Sub

    Private Sub SimpleButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSIMPAN.Click
        Dim cek As String = "insert"

        Dim eksekusi As String = ""
        Dim eksekusi2 As String = ""
        Dim keterangan As String = ""
        Dim bf As New BinaryFormatter
        Dim ms As New MemoryStream
        Dim bytearr As Byte()
        If meKETERANGAN.Lines.Count > 0 Then
            ms = New MemoryStream
            bf.Serialize(ms, meKETERANGAN.Lines)
            bytearr = ms.ToArray
            Dim vmemo As String = String.Empty
            For i As Integer = 0 To meKETERANGAN.Lines.Count - 1
                vmemo += meKETERANGAN.Lines(i).ToString
                If i < meKETERANGAN.Lines.Count - 1 Then
                    vmemo += "\n"
                End If
            Next
            keterangan = vmemo
            vKETERANGAN = replaceInput(vmemo)
        Else
        End If
        If isTambah = True Then
            eksekusi = "insert"
            eksekusi2 = "Menambah"
        Else
            eksekusi = "update"
            eksekusi2 = "Mengubah"
        End If
        If cekValidate(eksekusi) Then
            If eksekusi = "insert" Then

                If Not cani Then
                    MsgBox("Maaf Hak Akses Anda Tidak Terdaftar", MsgBoxStyle.Exclamation)
                    ClearLayout("2")
                    Exit Sub
                End If
            ElseIf eksekusi = "update" Then
                If Not canu Then
                    MsgBox("Maaf Hak Akses Anda Tidak Terdaftar", MsgBoxStyle.Exclamation)
                    ClearLayout("2")
                    Exit Sub
                End If
            End If
            If MsgBox("Anda Yaking Ingin " & eksekusi2 & " Data Riwayat Kesehatan Dengan" & vbCrLf _
                & "NIS" & vbTab & vbTab & vbTab & ": " & txtNIS.Text & vbCrLf _
                & "Sakit" & vbTab & vbTab & vbTab & ": " & txtSAKIT.Text & vbCrLf _
                & "Tanggal Mulai" & vbTab & vbTab & ": " & deMULAI.DateTime.ToString("dd/MM/yyyy") & vbCrLf _
                & "Tanggal Selesai" & vbTab & vbTab & ": " & deSELESAI.DateTime.ToString("dd/MM/yyyy") & vbCrLf _
                & "Keterangan" & vbTab & vbTab & ": " & meKETERANGAN.Text & vbCrLf _
                , MsgBoxStyle.OkCancel, "Informasi") = MsgBoxResult.Ok Then
                SimpanData(eksekusi)
                btnCARI_Click(sender, e)
                ClearLayout("2")
            End If
        Else
            Exit Sub
        End If
        
    End Sub
    Dim cani As Boolean = False
    Dim canu As Boolean = False
    Dim cand As Boolean = False
    Private Sub FormRiwayatKesehatan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LocalSession.SessForm = "RiwayatKesehatan"
        deSELESAI.Properties.MaxValue = Now
        deMULAI.Properties.MaxValue = Now
        If LocalSession.NisRiwKesehatan = "" Then
            'Exit Sub
        Else
            txtNIS.Text = LocalSession.NisRiwKesehatan
            txtNAMA.Text = LocalSession.NamaRiwKesehatan
            LocalSession.NisRiwKesehatan = ""
            LocalSession.NamaRiwKesehatan = ""
            ''setControlAktif(False)
            btnCARI_Click(sender, e)
            'txtUMUR_INPUT.Focus()
        End If
        Dim dr() As DataRow
        If Utils.RowExist(LocalSession.dtrole) Then
            dr = LocalSession.dtrole.Select("menuid = '" & LocalSession.PublicMenuId & "'")
            If dr.Length > 0 Then
                '       dt = dr.CopyToDataTable
                cani = IIf(IsDBNull(dr(0)("to_insert")), 0, dr(0)("to_insert"))
                canu = IIf(IsDBNull(dr(0)("to_update")), 0, dr(0)("to_update"))
                cand = IIf(IsDBNull(dr(0)("to_delete")), 0, dr(0)("to_delete"))
            End If
        End If
    End Sub

    Private Sub GetLoadDataSiswa(ByRef dtkesehatansiswa As DataTable)
        Dim strQuery As String()
        Dim err As String = ""
        strQuery = New String() {"*", "datasiswa", "nis", txtNIS.Text}
        If Utils.executeSP("proc_searchallEqual", strQuery, dtkesehatansiswa) Then
            dtkesehatansiswa = dtkesehatansiswa
        End If
    End Sub

    Private Sub GetLoadDataRiwayatSiswa(ByRef dtriwayatkesehatansiswa As DataTable)
        Dim strQuery As String()
        Dim err As String = ""
        strQuery = New String() {"*", "riwayatkesehatan", "nis", txtNIS.Text}
        If Utils.executeSP("proc_searchallEqual", strQuery, dtriwayatkesehatansiswa) Then
            dtriwayatkesehatansiswa = dtriwayatkesehatansiswa
        End If
    End Sub

    Private Sub SimpanData(ByVal jnsSimpan As String)
        Dim strQuery As Object()
        Dim err As String = ""
        inisial()
        Dim dtResulExecute As New DataTable
        Dim ValResult As String = ""

        strQuery = New Object() {jnsSimpan, vID, vSAKIT, vTGLMULAI, vTGLSELESAI, vKETERANGAN}
        If Utils.executeSP("proc_riwayatkesehatan", strQuery, dtResulExecute) Then
            If Utils.RowExist(dtResulExecute) Then
                Dim MsgResult As String = "Execute Successfull"

                If MsgResult = dtResulExecute(0)(0) Then
                    MsgBox(MsgResult, MsgBoxStyle.Information, "Success")
                    If isTambah = True Or jnsSimpan = "delete" Then

                    End If
                Else
                    MsgBox(dtResulExecute(0)(0), MsgBoxStyle.Critical, "Error")
                End If
            End If
        End If

    End Sub


    Private Sub GridView1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridView1.Click
        isTambah = False
        Dim obj As DataRow
        Dim i As Integer
        Dim j As Integer
        j = GridView1.RowCount
        If j > 0 Then
            i = GridView1.FocusedRowHandle
            obj = GridView1.GetDataRow(i)
            txtSAKIT.Text = obj(1)
            deMULAI.Text = obj(2).ToString
            deSELESAI.Text = obj(3).ToString
            meKETERANGAN.Text = obj(4)
            setControlAktif(3)
        End If

    End Sub

    Private Sub setControlAktif(ByVal aktif As Integer)
        If aktif = 1 Then
            deMULAI.Enabled = True
            deSELESAI.Enabled = True
            txtSAKIT.Properties.ReadOnly = False
            meKETERANGAN.Properties.ReadOnly = False
            btnHAPUS.Enabled = False
            btnSIMPAN.Enabled = True
        ElseIf aktif = 2 Then
            deMULAI.Enabled = False
            deSELESAI.Enabled = False
            txtSAKIT.Properties.ReadOnly = True
            meKETERANGAN.Properties.ReadOnly = True
            btnHAPUS.Enabled = False
            btnSIMPAN.Enabled = False
        Else
            deMULAI.Enabled = False
            deSELESAI.Enabled = False
            txtSAKIT.Properties.ReadOnly = False
            meKETERANGAN.Properties.ReadOnly = False
            btnHAPUS.Enabled = True
            btnSIMPAN.Enabled = True
        End If

    End Sub

    Private Sub btnHAPUS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHAPUS.Click
        If cekValidate("delete") Then
            If Not cand Then
                MsgBox("Maaf Hak Akses Anda Tidak Terdaftar", MsgBoxStyle.Exclamation)
                ClearLayout("2")
                Exit Sub
            End If
            If MsgBox("Anda Yaking Ingin Menghapus Data Riwayat Kesehatan Dengan" & vbCrLf _
                & "NIS" & vbTab & vbTab & vbTab & ": " & txtNIS.Text & vbCrLf _
                & "Sakit" & vbTab & vbTab & vbTab & ": " & txtSAKIT.Text & vbCrLf _
                & "Tanggal Mulai" & vbTab & vbTab & ": " & deMULAI.DateTime.ToString("dd/MM/yyyy") & vbCrLf _
                & "Tanggal Selesai" & vbTab & vbTab & ": " & deSELESAI.DateTime.ToString("dd/MM/yyyy") & vbCrLf _
                & "Keterangan" & vbTab & vbTab & ": " & meKETERANGAN.Text & vbCrLf _
                , MsgBoxStyle.OkCancel, "Informasi") = MsgBoxResult.Ok Then
                SimpanData("delete")
                btnCARI_Click(sender, e)
                ClearLayout("2")
            End If
        Else
            Exit Sub
        End If
        
    End Sub

    Private Sub deMULAI_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deMULAI.EditValueChanged
        deSELESAI.Properties.MinValue = deMULAI.DateTime
    End Sub

    Private Sub refreshGrid(ByVal dtriwayatkesehatansiswa As DataTable)
        If Utils.RowExist(dtriwayatkesehatansiswa) Or dtriwayatkesehatansiswa.Rows.Count = 0 Then
            GridControl1.DataSource = dtriwayatkesehatansiswa
            GridControl1.Refresh()
            GridControl1.RefreshDataSource()
            GridView1.RefreshData()
        Else
            GridControl1.DataSource = Nothing
            GridControl1.Refresh()
            GridControl1.RefreshDataSource()
            GridView1.RefreshData()
        End If
    End Sub

    Private Sub btnTAMBAH_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTAMBAH.Click
        If Not cani Then
            MsgBox("Maaf Hak Akses Anda Tidak Terdaftar", MsgBoxStyle.Exclamation)
            ClearLayout("2")
            Exit Sub
        End If
        setControlAktif(1)
        ClearLayout("2")
        isTambah = True
    End Sub

    Private Sub inisial()
        vID = replaceInput(txtNIS.Text)
        vSAKIT = replaceInput(txtSAKIT.Text)        
        vTGLMULAI = deMULAI.DateTime.ToString("yyyy-MM-dd")
        vTGLSELESAI = deSELESAI.DateTime.ToString("yyyy-MM-dd")
    End Sub

    Private Function replaceInput(ByVal teks As String) As String
        teks = teks.Replace("\", "\\")
        teks = teks.Replace("'", "\'")
        Return teks
    End Function

    Private Function cekValidate(ByVal jnsCEK As String) As Boolean
        Dim hasilcek As Boolean = True
        If Trim(txtNIS.Text) = "" Or Trim(deMULAI.Text) = "" Or Trim(deMULAI.Text) = "" Then
            hasilcek = False
            MsgBox("NIS atau Tanggal Riwayat Kesehatan Belum Diisi.", MsgBoxStyle.OkOnly, "Informasi")
        End If
        If jnsCEK = "insert" Or jnsCEK = "update" Then
            If Trim(txtSAKIT.Text) = "" Then
                hasilcek = False
                MsgBox("Pengisian Data Riwayat Kesehatan Belum Lengkap.", MsgBoxStyle.OkOnly, "Informasi")
            End If
        Else

        End If
        Return hasilcek
    End Function

End Class
